const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 询问开启权限
const Authorize = (scope) => {
  return new Promise((resolve, reject) => {
    wx.authorize({
      scope: scope,
      success: function (res) {
        resolve(res, scope);
      },
      fail: function (err) {
        reject(err, scope);
      }
    })
  });

}

// 获取canvas图片
const getCanvasPic = (canvas, canvasId) => {
  return new Promise((resolve, reject)=>{
    wx.canvasToTempFilePath({
      x: canvas.left,
      y: canvas.top,
      width: canvas.width,
      height: canvas.height,
      destWidth: canvas.width * canvas.pixelRatio,// 提高图片清晰度
      destHeight: canvas.height * canvas.pixelRatio,// 提高图片清晰度
      canvasId: canvasId,
      fileType: 'jpg',
      quality: 1,
      success: function (res) {
        // console.log(res.tempFilePath)
        // 生成图片成功
        resolve(res);
      },
      fail: function (err) {
        reject(err);
      }
    })
  });
}
// 获取屏幕信息
export const getPhoneinfo = () => {
  return new Promise(()=>{
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.pixelRatio)
      }
    });
  });
}

module.exports = {
  formatTime: formatTime,
  Authorize,
  getCanvasPic
}

